<template>
  <div class="call-special">
      <div class="call-special-box">
          <button @click="changePickUser">{{pickUser}}</button>
          <cube-button @click="showPicker">{{title}}</cube-button>
          <div>
            <router-link to="/estimatePrice">
              <p>约<span>{{moneys}}</span>元</p>
              <p>暂无优惠活动</p>
            </router-link>
          </div>
      </div>
      <cube-button @click="sureOrder">{{sureCarpool}}</cube-button>
  </div>
</template>

<script>
/* eslint-disable */
import axios from 'axios'
import {mapGetters,mapMutations} from 'vuex'
import {getFlightPrice,getCarType} from 'api/api'
let col1Data = []
export default {
  props: ['money'],
  data () {
    return {
      pickUser: '更换乘车人',
      title: '选择车型',
      sureCarpool: '确认拼单',
      carlevel:'',
      moneys:localStorage.getItem('prePrice')
    }
  },
  computed: {
    ...mapGetters([
      'carTypes',
      'flightInfo',
      'carStartTime',
      'pickType',
      'arrivalInfo',
      'orderName',
      'orderPhone'
    ])
  },
  mounted () {

  },
  methods: {
    // 选择乘车人
    changePickUser () {
      this.$router.push('/carrider')
    },
    // 确认下单
    sureOrder () {
      if(!this.orderName || !this.orderPhone) {
          this.$createDialog({
              type: 'alert',
              title: '',
              content: '请选择乘车人',
              icon: 'cubeic-alert'
          }).show()
      }else {
          this.setShowTips(true)
      }

    },
    curpiker () {
      let _this = this
      this.picker = this.$createPicker({
      title: '选择车型',
      data: [col1Data],
      onSelect: (selectedVal, selectedIndex, selectedText) => {
        const carid = Number(selectedVal[0]) ? Number(selectedVal[0]) : 2
        localStorage.setItem('carType',carid)
        if(localStorage.getItem('pickType') == 1){
            axios.post(getFlightPrice, {
                "param":{
                    "fields":{
                        "flightNo":localStorage.getItem('flightInfo'),
                        "date":localStorage.getItem('carStartTime'),
                        "carLevel":carid,
                        "serviceType":localStorage.getItem('pickType'),
                        "preTime":localStorage.getItem('timeConsuming'),
                        "distance":Number(localStorage.getItem('dsitence')),
                        "timeStamp":localStorage.getItem('arrivalInfo')
                    }
                }
            }).then((res) => {
                console.log(res)
                if(res.statusText === 'OK'){
                    localStorage.setItem('prePrice',res.data.result.prePrice)
                    _this.moneys = localStorage.getItem('prePrice')
                    _this.$nextTick()
                }
            }).catch(error => {
                console.log(error)
            })
        }else if(localStorage.getItem('pickType') == 2){
            axios.post(getFlightPrice, {
                "param":{
                    "fields":{
                        "flightNo":'',
                        "date":'',
                        "carLevel":carid,
                        "serviceType":localStorage.getItem('pickType'),
                        "preTime":localStorage.getItem('timeConsuming'),
                        "distance":Number(localStorage.getItem('dsitence')),
                        "timeStamp":localStorage.getItem('arrivalInfos')
                    }
                }
            }).then((res) => {
                if(res.statusText === 'OK'){
                    localStorage.setItem('prePrice',res.data.result.prePrice)
                    _this.moneys = localStorage.getItem('prePrice')
                    _this.$nextTick()
                }
            }).catch(error => {
                console.log(error)
            })
        }
      },
      onCancel: () => {

      }
    })
    },
    // 选择车型
    showPicker () {
      let _this = this
      axios.get(getCarType).then((res) => {
           let datas = []
           res.data.result.forEach(item => {
              let arrdata = {
                text:item.carType,
                value:item.carLevel
              }
              datas.push(arrdata)
           })
           col1Data = datas
           _this.curpiker()
           _this.picker.show()
        }
      )
    },
    ...mapMutations({
      setShowTips: 'SET_SHOWTIPS',
      setCarType: 'SET_CARTYPE',
      setFlightInfo: 'SET_FLIGHTINFO',
      setCarStartTime: 'SET_CARSTARTTIME',
      setPickType: 'SET_PICKTYPE',
      setArrivalInfo: 'SET_ARRIVALINFO',
      setOrderName: 'SET_ORDERNAME',
      setOrderPhone: 'SET_ORDERPHONE'
    })
  }
}
</script>

<style lang="css" scoped>
.call-special{
    position: fixed;
    bottom: 0;
    width: 90%;
    margin-left:5%;
}
.call-special .call-special-box{
     width: 100% ;
     margin-bottom: 10px;
      box-shadow: 0px 0px 20px #d9dee1
}
.call-special .call-special-box button{
    width: 50%;
    float: left;
    border: 0;
    border-bottom: 1px solid #cccccc;
    height: 50px;
    background-color: #ffffff;
    outline: none;
}
.call-special .call-special-box .cube-btn{
    color: #000;
    font-size: 14px;
}
.call-special .call-special-box button:nth-of-type(1){
    border-right: 1px solid #cccccc;
}
.call-special .call-special-box div{
    width: 100%;
    background-color: #fff;
    margin-bottom: 10px;
    padding-top:60px;
}
.call-special .call-special-box div p:nth-of-type(1){
    text-align: center;
    font-size: 16px;
    line-height: 30px;
}
.call-special .call-special-box div p:nth-of-type(1) span{
font-size: 24px;
}
.call-special .call-special-box div p:nth-of-type(2){
    font-size: 12px;
    line-height: 30px;
}
</style>